package operate;

import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 05 - 10
 * Time: 20:52
 */
public class DelOperate implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要删除的图书的名字");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int currentNums = bookList.getBooknumbers();
        int target = -1;
        for (int i = 0; i < currentNums; i++) {
            if(bookList.getBook(i).getName().equals(bookName)) {
                target = i;
                break;
            }
        }
        if(target == -1) {
            System.out.println("没有你要删除的书");
        }
        for (int i = target; i < currentNums - 1; i++) {
            bookList.setBook(i, bookList.getBook(i + 1));
        }
        bookList.setBooknumbers(currentNums - 1);
        System.out.println("删除图书成功");
    }
}
